#include <iostream>
#include <vector>
#include <algorithm>
#include <stack>
using namespace std;

int numRescueBoats(vector<int>& people, int limit) {
    sort(people.begin(), people.end());
    int i = 0;
    int j = people.size() - 1;
    int num = 0;
    
    while (i <= j) {
        if (people[i] + people[j] <= limit) {
            num += 1;
            i += 1;
            j -= 1;
            
        } else if (people[i] + people[j] > limit) {
            num += 1;
            j -= 1;
            
        }
    }
    return num;
}

int main(){

    vector<int> people = {3,5,3,4};
    int limit = 5;
    cout << numRescueBoats(people, limit) << endl;
    return 0;
}